/*
 * @Descripttion:
 * @version:
 * @Author: liyuhang
 * @Date: 2022-05-19 10:27:55
 */

Vue.component("home-page", {
    template:
        '\<div class="mui-content">\
            <div class="is-loading">\
                <div class="curtain">\
                    <div class="loadingBgc">\
                        <div class="loader"></div>\
                    </div>\
                </div>\
            </div>\
            <div class="bgcDiv1">\
                <van-search shape="round" v-model="value1" placeholder="请输入搜索关键词" @click="onSearch" />\
            </div>\
            <div class="functionsif">\
                <div class="single" v-for="(item, index) in NavigationList" :key="index" @click="convert(item.id)">\
                    <div class="imgDiv">\
                        <img :src="item.imgSrc" alt="" class="imgSrc" />\
                    </div>\
                </div>\
            </div>\
            <div class="current">\
                    <van-tabs class="myDocument" v-model="active">\
                        <van-tab title="我的">\
                            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load=\'getList(0,"")\'>\
                                <van-cell v-for="item in list" :clickable="true">\
                                    <div class="wrpper">\
                                        <div class="img_box">\
                                            <img v-if=\'item.documentType == "Word"\' src="../image/newDocument.png" alt="">\
                                            <img v-if=\'item.documentType == "Excel"\' src="../image/newDocument1.png" alt="">\
                                            <img v-if=\'item.documentType == "TXT"\' src="../image/newDocument4.png" alt="">\
                                            <img v-if=\'item.documentType == "PPT"\' src="../image/newDocument2.png" alt="">\
                                            <img v-if=\'item.documentType == "PDF"\' src="../image/newDocument3.png" alt="">\
                                            <img v-if=\'item.documentType == "WPS"\' src="../image/wps.png" alt="">\
                                        </div>\
                                        <div class="text_h">\
                                            <div class="List_title">{{item.documentName}}</div>\
                                            <div class="List_wrapper">\
                                            <div class="List_time">\
                                            <span class="List_name">{{item.createTime}}</span><span class="lines_c"></span><span class="List_name">{{ item.fileSize }}</span>\
                                                      </div>\
                                            </div>\
                                        </div>\
                                    </div>\
                                </van-cell>\
                            </van-list>\
                        </van-tab>\
                    </van-tabs>\
                <div style="position: fixed;bottom: 10%;right: 10px;display: flex;flex-wrap: wrap;width: 60px;">\
                    <div class="single" v-for="(item, index) in functionsifList" :key="index" @click="functionBtn(index, item)">\
                        <img width="60px" :src="item.imgSrc" alt="" class="imgSrc" />\
                    </div>\
                 <input type="file" multiple="false" accept=".pptx,.doc,.txt,.docx,.ppt,.xls,.xlsm,.xlsx,.wps" @change="getFile" style="position: absolute; bottom: 0;line-height: 1.1rem; width: 100%;height: 1.3rem;font-size: 30px;opacity: 0;" ref="multipartFile1" name="multipartFile1" id="multipartFile1" />\
                </div>\
            </div>\
            <div>\
                <van-popup round v-model="show" closeable style="width:80%;max-height: 50%;padding:10px;">\
                    <div class="title" style="height: 50px;overflow: hidden;">\
                        <span>{{ gaugeOutfit }}列表</span>\
                    </div>\
                    <div class="tabulation">\
                        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load=\'getList(0,gaugeOutfit)\'>\
                            <van-cell  v-for="item in tabulation"  :clickable="true">\
                                <div class="wrpper" style="margin: 10px 5px;" @click="convertDocument(item)">\
                                    <div  class="img_box">\
                                        <img v-if=\'gaugeOutfit == "Word"\' src="../image/newDocument.png" alt="">\
                                        <img v-if=\'gaugeOutfit == "Excel"\' src="../image/newDocument1.png" alt="">\
                                        <img v-if=\'gaugeOutfit == "PPT"\' src="../image/newDocument2.png" alt="">\
                                        <img v-if=\'gaugeOutfit == "PDF"\' src="../image/newDocument3.png" alt="">\
                                        <img v-if=\'gaugeOutfit == "TXT"\' src="../image/newDocument4.png" alt="">\
                                        <img v-if=\'gaugeOutfit == "WPS"\' src="../image/wps.png" alt="">\
                                    </div>\
                                    <div class="text_h" style="margin-left: 5px;">\
                                        <div class="List_title" style="left: 16%;">{{item.documentName}}</div>\
                                        <div class="List_wrapper" style="left: 20%;">\
                                            <div class="List_time"><span>{{item.createTime}}</span><span class="lines">𐊊</span><span>{{ item.fileSize }}</span></div>\
                                        </div>\
                                    </div>\
                                </div>\
                            </van-cell>\
                        </van-list>\
                    </div>\
                </van-popup>\
            </div>\
            <div>\
            <van-popup class="popBox" v-model="showPop" closeable @close="closePop">\
             <div style=" height: 100%;">\
             <textarea class="textWrappper" maxlength="100" placeholder="请输入文本内容" v-model="oldText"></textarea>\
             <div class="btn_Box">\
              <van-button @click="writeFun(1)" size="small" type="primary">文本续写</van-button>\
              <van-button @click="writeFun(2)" size="small" type="warning">文本纠错</van-button>\
              <van-button @click="writeFun(3)" size="small" type="info">近义词提示</van-button>\
             </div>\
             <div v-if="numAct==2" class="allBoxText">\
             <p v-for="(item,index) in newText">\
              <span v-for="i in item"><span style="margin-right:5px;">{{i}}</span>\
             </span>\
            </p>\
             </div>\
             <div v-else class="allBoxText">\
              <p v-for="(item,index) in newText">\
              {{item}}</p>\
              </div>\
             <div class="btn_Box" style="margin-top:10px;">\
              <van-button @click="copy" class="tag-read" size="small" type="primary">复制</van-button>\
             </div>\
             </div>\
            </van-popup>\
          </div>\
        </div>\
          ',
    data() {
        return {
            value1: '',
            active: 0,
            show: false,
            searVal: false,
            oldText:'',
            numAct:0,
            newText: [],
            list: [],
            loading: false,
            finished: true,
            NavigationList: [
                {
                    id: 1,
                    imgSrc: "../image/scene.png",
                    acceptType: "image/png",
                },
                {
                    id: 2,
                    imgSrc: "../image/scene1.png",
                    acceptType: "",
                },
                {
                    id: 3,
                    imgSrc: "../image/scene2.png",
                    acceptType: "image/png",
                },
                {
                    id: 4,
                    imgSrc: "../image/txtWord.png",
                    acceptType: "image/png",
                    // style: "margin-top: -1.4rem"
                },
                {
                    id: 5,
                    imgSrc: "../image/scene4.png",
                    acceptType: "image/png",
                    // style: "margin-top: -1.4rem"
                },
                {
                    id: 6,
                    imgSrc: "../image/scene6.png",
                    style: "opacity: 0"
                }
            ],
            functionsifList: [
                {
                    id: 1,
                    imgSrc: "../image/word.png",
                    acceptType: "image/png",
                },
                {
                    id: 2,
                    imgSrc: "../image/mistake.png",
                    acceptType: "image/png",
                },
                {
                    id: 3,
                    imgSrc: "../image/upLoad.png",
                    acceptType: "image/png",
                },
            ],
            num: 0,
            // 弹出层
            showPop: false,
            // 表头
            gaugeOutfit: '',
            // 列表
            tabulation: [],
            //转化后的各式
            convertDocumentSuffix: ''
        };
    },
    created() {
        this.getList(0, '')
    },
    methods: {
         // 续写纠错近义词
         writeFun(num) {
            console.log(num)
            if(!this.oldText || this.oldText.split(" ").join('').length == 0) {
                mui.toast("输入内容不能为空", {
                    duration: 'long',
                    type: 'div'
                  })
                  return
            }
            this.newText = []
            this.numAct = num
            $(".curtain").css("display", "block");
            let timestamp = new Date().getTime() + "";
            if (sessionStorage.getItem("iscp_port")) {
              iscp_port = sessionStorage.getItem("iscp_port");
            } else {
              iscp_port = "";
            }
            var URL
           if(num == 2) {
            URL = localUrlLink + iscp_port + '/document/news/pycorrector_v1'
           } else if(num == 1){
            URL = localUrlLink + iscp_port + '/document/news/automatic_continuation'
           } else {
            URL = localUrlLink + iscp_port + '/document/news/nlp_synonym'
           }
           console.log(URL)
           let data = {
              timestamp: timestamp,
              txt: this.oldText,
              text: this.oldText,
            };
  
            data = JSON.stringify(data);
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            $.ajax({
              url: URL,
              type: "post",
              async: false, //是否同步，false表示同步请求
              dataType: "json",
              data: data,
              headers: {
                "Content-Type": "application/json;charset=UTF-8",
                Authentication: AuthenticationSession,
                sign: sign,
                timestamp: timestamp
              },
              success: (res, txt, xhr) => {
                $(".curtain").css("display", "none");
                if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                  let Authentication = xhr.getResponseHeader("Authentication")
                  if (res.code == 200) {
                    sessionStorage.setItem("Authentication", Authentication);
                    if(num == 1) {
                       this.newText = JSON.parse(res.resultValue) 
                       delete this.newText.timestamp
                       console.log( this.newText,222)
                       console.log( this.newText.text0.length,333)
                       if(this.newText.text0 == " "){
                       console.log(this.numAct)
                        this.newText = []
                       }
                    } else if(num == 3) {
                      this.newText = JSON.parse(res.resultValue).synonym_10
                    } else{
                      this.newText = JSON.parse(res.resultValue)['err-correct']
                    }
                    if(!this.newText || this.newText.length == 0) {
                        console.log(8888)
                        this.newText = ["无"]
                    }
                    console.log(this.newText,555)
                  } else  {
                    mui.toast(res.resultValue, {
                      duration: 1000,
                      type: 'div'
                    })
                  }
                } else {
                  mui.toast('完整性校验失败', {
                    duration: 1000,
                    type: 'div'
                  })
                }
              },
              error: (e) => {
                $(".curtain").css("display", "none");
                mui.toast('请求异常,请稍后再试', {
                  duration: 1000,
                  type: 'div'
                })
              },
            });
          },
          copy() {
            var text1 = JSON.stringify(this.newText);
            console.log(text1)
                      text1 = text1.replace(/"/g, "");
                      text1 = text1.replace(/{/g, "");
                      text1 = text1.replace(/}/g, "");
                      text1 = text1.replace(/\[|]/g, "");
                      // text2 = text2.replace(/"/g, "");
                      // text2 = text2.replace(/{/g, "");
                      // text2 = text2.replace(/}/g, "");
                      // var text3 = text1 + "," + text2;
                      text1 = text1.replace(/,/g, "\n");
                      var clipboard = new Clipboard(".tag-read", {
                          // 通过target指定要复印的节点
                          text: function () {
                              return text1;
                          },
                      });
                      clipboard.on("success", (e) => {
                          mui.toast("复制成功", {
                              duration: "long",
                              type: "div"
                          });
                        
                      });
                      clipboard.on("error", (e) => {
                         
                      });
          },
          closePop(){
            this.showPop= false,
            this.newText = []
            this.oldText = ''
          },
          writeBtn() {
            this.showPop= true
          },
        getList(num, name) {
            userId = sessionStorage.getItem("userId");
            if (sessionStorage.getItem("iscp_port")) {
                iscp_port = sessionStorage.getItem("iscp_port");
            } else {
                iscp_port = "";
            }
            this.num += 1
            let timestamp = new Date().getTime() + "";
            let data = {
                timestamp: timestamp,
                userId: userId,
                pageNum: this.num,
                type: name,
                suffix: '',
            };
            data = JSON.stringify(data);
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            $.ajax({
                url: localUrlLink + iscp_port + '/document/record/getDocumentList',
                type: "post",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign,
                    timestamp: timestamp
                },
                success: (res, txt, xhr) => {
                    $(".curtain").css("display", "none");
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            sessionStorage.setItem("Authentication", Authentication);
                            this.total = res.resultValue.total
                            if (this.total == 0) {
                                this.finished = true;
                            }
                            if (res.resultValue.list.length < 10) {
                                this.finished = true;
                            }
                            if (name == '') {
                                if (this.num == 1) {
                                    this.list = []
                                    // this.tabulation = []
                                }
                                res.resultValue.list.forEach((item) => {
                                    this.list.push(item)
                                    // this.tabulation.push(item)
                                })
                                // 加载状态结束
                                this.loading = false;
                                if (this.list.length >= res.resultValue.total) {
                                    this.finished = true;
                                }
                            } else {
                                if (this.num == 1) {
                                    // this.tabulation = []
                                }
                                res.resultValue.list.forEach((item) => {
                                    this.tabulation.push(item)
                                })
                                // 加载状态结束
                                this.loading = false;
                                if (this.tabulation.length >= res.resultValue.total) {
                                    this.finished = true;
                                }
                            }
                            // }
                        } else {
                            mui.toast(res.resultValue, {
                                duration: 1000,
                                type: 'div'
                            })
                        } 
                    } else {
                        mui.toast('完整性校验失败', {
                            duration: 1000,
                            type: 'div'
                        })
                    }
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请求异常,请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },

        functionBtn(index, item) {
            console.log(item.id)
            // if (JSON.parse(sessionStorage.getItem("integralTotal")).ocr > 0) {
            //   this.addedPreviewFile = "";
            if (item.id == 1) {
                $(location).attr("href", "newFile.html");
            }
            else if(item.id == 2){
                this.writeBtn()
                // mui.toast("暂未开放！", { duration: "long", type: "div" });
            // }
            } else {
                mui.toast("暂未开放！", { duration: "long", type: "div" });
            }
        },
        // 转换
        convert(id) {
            this.gaugeOutfit = ''
            if (id == 1) {
                console.log("word转PDF");
                this.gaugeOutfit = 'Word'
                this.convertDocumentSuffix = 'PDF'
                this.show = true;
            } else if (id == 2) {
                console.log("excel转PDF");
                this.gaugeOutfit = 'Excel'
                this.convertDocumentSuffix = 'PDF'
                this.show = true;
            } else if (id == 3) {
                console.log("PPT转PDF");
                this.gaugeOutfit = 'PPT'
                this.convertDocumentSuffix = 'PDF'
                this.show = true;
            } else if (id == 4) {
                console.log("Txt转word");
                this.gaugeOutfit = 'TXT'
                this.convertDocumentSuffix = 'PDF'
                this.show = true;
                // mui.toast("暂未开放！", { duration: "long", type: "div" });
                // return
            } else if (id == 5) {
                $(location).attr("href", "scanFile.html");
                this.gaugeOutfit = '文件扫描'
                // mui.toast("暂未开放！", { duration: "long", type: "div" });
                return
            } else {
                console.log("证件扫描");
                $(location).attr("href", "documentFile.html");
                this.gaugeOutfit = '证件扫描'
                // mui.toast("暂未开放！", { duration: "long", type: "div" });
                return
            }
            this.tabulation = []
            this.num = 0
            this.getList(0, this.gaugeOutfit)
        },
        //上传
        getFile() {
            this.show = false;
            var files
            files = document.getElementById("multipartFile1")
            files.removeAttribute("multiple");
            if (files.files.length > 1) {
                mui.toast("单次只可上传一个文件", {
                    duration: 'long',
                    type: 'div'
                })
                return;
            }
            var list =["pptx","doc", "txt", "docx", "ppt", "xls", "xlsm", "xlsx", "wps"]
            var name = files.files[0].name.toLowerCase()
            const suffix = name.split('.').reverse()[0]
            if(!list.includes(suffix.toLowerCase())){
                files.value = null
                mui.toast("文件格式不正确，请重新上传", {
                            duration: 'long',
                            type: 'div'
                    })
                 return
            }
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            let iscp_port
            if (sessionStorage.getItem("iscp_port")) {
                iscp_port = sessionStorage.getItem("iscp_port");
            } else {
                iscp_port = ""
            }
            var resultFile = files.files[0];
            $(".curtain").css("display", "block");
            //传递的表单数据
            var form = new FormData();
            // var form = new FormData($("#form-user")[0]);
            unitId = sessionStorage.getItem("unitId");
            userId = sessionStorage.getItem("userId");
            var timestamp = new Date().getTime() + "" //时间戳
            form.append("userId", userId);
            form.append("multipartFile", resultFile);
            // form.append("unitId", unitId);
            let sign3 = sm3(timestamp + "&" + userId)
            $.ajax({
                url: localUrlLink + iscp_port + '/document/record/documentsUpload',
                type: "post",
                // async: false, //是否同步，false表示同步请求
                // dataType: 'json',
                data: form,
                cache: false,
                timeout: 120000,
                contentType: false,
                processData: false,
                headers: {
                    // "Content-Type": "application/json;charset=UTF-8",
                    sign: sign3,
                    userId: userId,
                    Authentication: AuthenticationSession,
                    timestamp: timestamp,
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            $(".curtain").css("display", "none");
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                                // if (num == 1) {
                                //     mui.toast("上传成功，请在内网邮箱中查收", {
                                //         duration: 'long',
                                //         type: 'div'
                                //     })
                                // } else {
                                //     mui.toast("上传成功", {
                                //         duration: 'long',
                                //         type: 'div'
                                //     })
                                //     this.getShorthandList(20)
                                // }
                            }
                            this.show = false;
                            this.num = 0
                            this.getList(0, '')
                            mui.toast("上传成功", {
                                duration: 'long',
                                type: 'div'
                            })
                            files.value = null
                        } else if (res.code == 500) {
                            $(".curtain").css("display", "none");
                            mui.toast(res.resultValue, {
                                duration: 'long',
                                type: 'div'
                            })
                            files.value = null
                        } else {
                            $(".curtain").css("display", "none");
                            mui.toast('请求异常,请稍后再试', {
                                duration: 'long',
                                type: 'div'
                            })
                            files.value = null
                        }
                    } else {
                        $(".curtain").css("display", "none");
                        mui.toast('完整性校验失败', {
                            duration: 'long',
                            type: 'div'
                        })
                        files.value = null
                    }

                },
                error: function (e) {
                    $(".curtain").css("display", "none");
                    files.value = null
                    mui.toast("请求异常，请稍后再试！", {
                        duration: 'long',
                        type: 'div'
                    })
                },
            });
        },
        // 转化选择按钮
        convertDocument(item) {
            $(".curtain").css("display", "block");
            userId = sessionStorage.getItem("userId");
            this.show = false
            // return
            let timestamp = new Date().getTime() + "";
            let data = {
                timestamp: timestamp,
                id: item.id,
                userId:userId,
                suffix: item.documentSuffix,
                convertSuffix: this.convertDocumentSuffix,
            };
            if (sessionStorage.getItem("iscp_port")) {
                iscp_port = sessionStorage.getItem("iscp_port");
            } else {
                iscp_port = "";
            }

            data = JSON.stringify(data);
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            var URL
            console.log(item.documentSuffix,"leixing")
            if(item.documentSuffix == ".txt") {
                URL = localUrlLink + iscp_port + "/document/transform/txtToPdf"   
            } else {
                URL = localUrlLink + iscp_port + '/document/transform/convertDocument'
            }
            $.ajax({
                url: URL,
                type: "post",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign,
                    timestamp: timestamp
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            sessionStorage.setItem("Authentication", Authentication);
                            // this.show = false
                            setTimeout(() => {
                                $(".curtain").css("display", "none");
                                mui.toast("转化成功", {
                                    duration: 'long',
                                    type: 'div'
                                })
                                this.num = 0
                                this.getList(0, '')
                            }, 3000);
                        } else {
                            $(".curtain").css("display", "none");
                            mui.toast(res.resultValue, {
                                duration: 1000,
                                type: 'div'
                            })
                        } 
                    } else {
                        $(".curtain").css("display", "none");
                        mui.toast('完整性校验失败', {
                            duration: 1000,
                            type: 'div'
                        })
                    }
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请求异常,请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        // 搜索按钮
        onSearch() {
            this.searVal = true
            this.$emit('custom-event', this.searVal);
            // this.navigation(1)
        },
    },
});
